package com.rongke.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by bin on 2018/4/5.
 */
public class ThreadPool {

    private static ThreadPoolExecutor threadPool;

    private static  Integer corePoolSize;

    private static  Integer maximumPoolSize;

    private static final TimeUnit unit = TimeUnit.MILLISECONDS;

    private static  Integer keepAliveTime;

    private static  BlockingQueue workQueue;




    public static ThreadPoolExecutor getThreadPoolExecutor(){
        return threadPool;
    }

    static {
        String path = ThreadPool.class.getClassLoader().getResource("").getPath()+ "/thread.properties";
        File file = new File(path);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(inputStream);
            corePoolSize = Integer.valueOf(properties.getProperty("thread.corePoolSize"));
            maximumPoolSize = Integer.valueOf(properties.getProperty("thread.maximumPoolSize"));
            keepAliveTime = Integer.valueOf(properties.getProperty("thread.keepAliveTime"));
            Integer queueCount = Integer.valueOf(properties.getProperty("thread.queueCount"));
            workQueue = new ArrayBlockingQueue(queueCount);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        threadPool = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue);
    }

    private ThreadPool(){
    }
}
